/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.aptech.DAO;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Hoang Quy Hong An
 */
public class myConnection {
  private static String url;
    private static String ServerName;
    private static String dbName;
    private static String userName = "sa";
    private static String password = "11";
    private static Connection cnn;

    public myConnection() {
        getConnection();
    }

    public  static String getConnectionUrl() {
        url = "jdbc:sqlserver://";

        ServerName = "localhost";

        //ServerName = "TRUNGKIEN-PC";

        dbName = "MySweetHome";

        return url + ServerName + ":1433" + ";databaseName=" + dbName;
    }

    public static Connection getConnection() {
        try {
            //  Class.forName(com.);
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            cnn = DriverManager.getConnection(getConnectionUrl(), userName, password);
            //System.out.println("Thanh cong!");
           return cnn;
        } catch (SQLException ex) {
            Logger.getLogger(myConnection.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(myConnection.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public static void closeConnection(Connection con, PreparedStatement ps, ResultSet rs) {
        try {
            if (rs != null) {
                rs.close();
            }
            if (ps != null) {
                ps.close();
            }
            if (con != null) {
                con.close();
            }
        } catch (Exception ex) {
        }
    }

//    public static void main(String[] args) {
//        myConnection.getConnection();
//    }

}
